package com.bsj.power.pls.mqtt.auth;

import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.core.server.auth.IMqttServerAuthHandler;
import org.tio.core.ChannelContext;
import org.tio.core.Node;

/**
 * @author yinhao
 * @version 1.0
 * @description 自定义mqtt认证
 * @date 2023/11/6
 */
@Slf4j
public class MqttAuthHandler implements IMqttServerAuthHandler {

    private static final String mqtt_username = "cnpower";
    private static final String mqtt_password = "cnpower";


    /**
     * 登录验证
     *
     * @param channelContext 连接管道
     * @param uniqueId       唯一ID
     * @param clientId       客户端ID
     * @param userName       用户名称
     * @param password       密码
     * @return
     * @desc 启用三元组鉴权模式
     * clientId： productKey.deviceName|securemode=2,signmethod=hmacsha256,timestamp=1699345650774|
     * username: deviceName&productKey
     * password: deviceSecret HmacSHA256 encode
     */
    @Override
    public boolean authenticate(ChannelContext channelContext, String uniqueId,
                                String clientId, String userName, String password) {
        Node clientNode = channelContext.getClientNode();
        long millis = System.currentTimeMillis();

        return mqtt_username.equals(userName) && mqtt_password.equals(password);
    }
}
